# -*- coding: utf-8 -*-

import random
import os
import re
import PathConfig
import time
import addRandomUI
import renamePropertyName
import copy
# 替换函数名 



# 所有方法名
funcList = []

# 路径信息
pathInfoList = []

# 所有函数名 新旧 数组
funNameList_old = []
funNameList_new = []

# 过滤掉的函数名
ignoreFunNameList = [
	'isEqual',
	'layoutSubviews',
	'awakeFromNib',
	'hitTest',
	'prepareForSegue',
	'reloadData',
	'dataArray',
	'backgroundColor',
	'backgroundView',
	'loadData',
	'back',
	'load',

	'clickContinue',
	'clickHead',
	'clickAction'
	]


ignoreFunHeadList = [
	'scrollView',
	'tableView',
	'cellFor',
	'collectionView',
	'view',
	'preferred',
	'gesture',
	'set',
	'init',
	'modelCustom',
	'dismissHUD',
	'numberOf',
	'pushView',
	'application',
	'imagePicker'
	]





def replaceAllFuncName():
	
	for pathInfo in pathInfoList:
		
		tmp_path = pathInfo['path']
		filename = pathInfo['name']

		file_str = ''
		with open(tmp_path) as f:
			file_str = f.read()
		
			for i in range(0,len(funNameList_old)):
				funNameOld = funNameList_old[i]
				funNameNew = funNameList_new[i]

				file_str = file_str.replace(funNameOld,funNameNew)

				# file_str = file_str.replace('set'+funNameOld.capitalize(),'set'+funNameNew.capitalize())
				# file_str = file_str.replace(' '+funNameOld,' '+funNameNew)
				# file_str = file_str.replace(')'+funNameOld,')'+funNameNew)
				# file_str = file_str.replace('('+funNameOld+')','('+funNameNew+')')
				
		with open(tmp_path, 'w') as f:
			f.write(file_str)

		print filename


# 获取所有方法
def getAllFuncName(isClean = False, ignorePropertyList = []):
	
	for pathInfo in pathInfoList:
		
		tmp_path = pathInfo['path']
		filename = pathInfo['name']

		with open(tmp_path) as f:
			file_str = f.read()				
			file_str = file_str.split('\n')
			for line in file_str:
				line = line.lstrip()
				if line.startswith('-') or line.startswith('+'):
					if line.find('(')>0 and line.find(')')>0 and line.find('IBAction')<0:
						splitStr = line.split(')')
						splitStr = splitStr[1].split(':')
						splitStr = splitStr[0].split('{')
						funcName = splitStr[0]
						funcName = funcName.replace(';','')
						funcName = funcName.replace(' ','')
						if funcName not in funNameList_old:

							# 过滤
							if funcName not in ignoreFunNameList:
								isIgnore = False
								for headStr in ignoreFunHeadList:
									if funcName.startswith(headStr):
										isIgnore = True

								if funcName in ignorePropertyList:
									isIgnore = True

								if not isIgnore:
									
									print '要替换的函数名：' + funcName
									
									# 新增一个函数名
									funNameList_old.append(funcName)

									# 过滤裁剪一下 旧版替换
									funcName = renamePropertyName.cleanRandomProperty(funcName)

									if isClean:
										funNameList_new.append(funcName)
									else:
										funcName_new = renamePropertyName.createRandomProperty(funcName)
										funNameList_new.append(funcName_new)
									
									# print funcName_new
			    

	

# 遍历所有路径
def traversePath(file_dir):
	
	fs = os.listdir(file_dir)
	for dir in fs:
		if dir.startswith('.'):
			#过滤隐藏
			continue
		

		tmp_path = os.path.join(file_dir, dir)
		if not os.path.isdir(tmp_path):

			if not dir.endswith('.h') and not dir.endswith('.m'):
				continue
			if dir.find(PathConfig.file_pre_class_type) < 0:
				# 不是以这个开头的文件过滤掉
				if dir.find('AppDelegate') < 0:
					continue

			pathInfoList.append({'name':dir,'path':tmp_path});
			
		else:
			# 是文件夹,则递归调用
			traversePath(tmp_path)


def beginRenameFuncName(passList):

	traversePath(PathConfig.rub_filepath)
	
	list = copy.copy(passList)

	getAllFuncName(False,list);

	
	# getAllFuncName(True);

	# print pathInfoList

	replaceAllFuncName();

# def main():

# 	traversePath(PathConfig.rub_filepath)
	
# 	getAllFuncName();
# 	# getAllFuncName(True);

# 	# print pathInfoList

# 	replaceAllFuncName();

# 	print('Finish done')

# if __name__ == '__main__':
# 	main()

